package cn.topcodes.tcsf.admin.service.impl;

import cn.topcodes.tcsf.admin.commons.domain.Page;
import cn.topcodes.tcsf.admin.commons.generic.GenericService;
import cn.topcodes.tcsf.admin.domain.entity.SystemProperty;
import cn.topcodes.tcsf.admin.service.SystemPropertyService;
import com.google.common.base.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.util.List;

/**
 * 系统属性管理服务
 * @author 大爱阳哥
 *
 */
@Service
@Transactional(readOnly=true, rollbackFor = Exception.class)
public class SystemPropertyServiceImpl extends GenericService<SystemProperty> implements SystemPropertyService {

	@Override
	@Transactional(readOnly=false, rollbackFor = Exception.class)
	public void createSysProp(String name, String value) {
		SystemProperty prop = new SystemProperty();
		prop.setName(name);
		prop.setValue(value);
		this.create(prop);
	}

	@Override
	@Transactional(readOnly=false, rollbackFor = Exception.class)
	public void modifySysProp(String name, String value) {
		SystemProperty prop = this.findSysProp(name);
		prop.setValue(value);
		this.update(prop);
	}

	/**
	 * 设置系统属性
	 * @param name
	 * @param value
	 */
	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public void setSysProp(String name, Object value) {
		SystemProperty prop = this.findSysProp(name);
		if(prop == null) {
			prop = new SystemProperty();
			prop.setName(name);
			Assert.notNull(value, "属性值不能为空");
			if(value instanceof Boolean) {
				prop.setType("Boolean");
			}else if(value instanceof Float || value instanceof Integer ||
					value instanceof Long || value instanceof Byte ||
					value instanceof Short || value instanceof Double) {
				prop.setValue("Number");
			}else {
				prop.setType("String");
			}
		}
		if(value != null) {
			PropertyEditor editor = PropertyEditorManager.findEditor(value.getClass());
			editor.setValue(value);
			prop.setValue(editor.getAsText());
		}else {
			prop.setValue(null);
		}
		this.save(prop);
	}

	@Override
	@Transactional(readOnly=false, rollbackFor = Exception.class)
	public void deleteSysProp(String name) {
		SystemProperty prop = this.findSysProp(name);
		this.delete(prop);
	}

	@Override
	public SystemProperty findSysProp(String name) {
		Example example = this.buildExample();
		//	TODO 字段名称定义为常量
		example.or().andEqualTo("name", name);
		SystemProperty prop = this.findOne(example);
		return prop;
	}

	@Override
	public List<SystemProperty> listSysProp(String name) {
		Example example = this.buildExample();
		example.or().andLike("name", "%" + name + "%");
		return this.findAll(example);
	}

	@Override
	public Page<SystemProperty> pageSysProp(int pageNumber, int pageSize, String name) {
		Example example = this.buildExample();
		example.or().andLike("name", "%" + name + "%");
		return this.findPage(example, pageNumber, pageSize);
	}

	@Override
	public boolean isAccessLogOn() {
		return this.findSysProperty(SYS_PROP_ACCESS_LOG_ON, Boolean.class, false);
	}

	@Override
	@SuppressWarnings("unchecked")
	public <T> T findSysProperty(String name, Class<T> clazz, T defaultValue) {
		if(Strings.isNullOrEmpty(name) || clazz == null) return defaultValue;
		SystemProperty prop = this.findSysProp(name);
		if(prop == null) return defaultValue;
		if(Strings.isNullOrEmpty(prop.getValue())) return defaultValue;
		PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
		if(editor == null) throw new IllegalArgumentException("系统属性不支持数据类型：" + clazz.getName());
		editor.setAsText(prop.getValue());
		Object obj = editor.getValue();
		if(obj == null) return defaultValue;
		return (T) obj;
	}

	
}
